package com.agm.android.binder;

import java.util.HashMap;
import java.util.List;

import com.example.android.navigationdrawerexample.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BinderGiocatore extends BaseAdapter {
	public static final String KEY_ID = "idcalciatore";
	public static final String KEY_COGNOME = "cognome";
	public static final String KEY_SQUADRE = "nomesquadra";
	public static final String KEY_GIOCATE = "giocate";
	public static final String KEY_RUOLO = "ruolo";
	public static final String KEY_IMAGE_STATO = "stato";
	public static final String KEY_NOME = "text";
	
	LayoutInflater inflater;
	ImageView list_image;
	List<HashMap<String, String>> contentDataCollection;
	ViewHolder holder;
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return  contentDataCollection.size();
	}

	public BinderGiocatore(Activity act, List<HashMap<String, String>> map) {
	    this.contentDataCollection = map;
	    inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public BinderGiocatore(LayoutInflater i, List<HashMap<String, String>> map) {
	    this.contentDataCollection = map;
	    inflater = i;
	}
	
	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
	    if (convertView == null) {

	    	view = inflater.inflate(R.layout.rowgiocatore, null);
	        holder = new ViewHolder();

	        holder.cognome = (TextView) view.findViewById(R.id.cognomegiocatore);
	        holder.ruolo = (TextView) view.findViewById(R.id.ruologiocatore);
	        holder.stato = (ImageView) view.findViewById(R.id.imagestato);

	        view.setTag(holder);
	    } else {
	        holder = (ViewHolder) view.getTag();
	    }
	    holder.cognome.setText(contentDataCollection.get(position).get(KEY_COGNOME));
	    holder.ruolo.setText(contentDataCollection.get(position).get(KEY_RUOLO));

	    String drawableName = contentDataCollection.get(position).get(KEY_IMAGE_STATO); 
	    int imageResource = view.getContext().getResources().getIdentifier(drawableName, "drawable", view.getContext().getPackageName());

	    Drawable image = view.getContext().getResources().getDrawable(imageResource);
	  
	    holder.stato.setImageDrawable(image);

	    return view;
	}
	
	/*
	 * 
	 * */
	static class ViewHolder {
		ImageView stato;
	    TextView cognome;
	    TextView ruolo;
	}

}
